/**
 * 表单设计--选择档案
 */

mini.parse();

var grid = mini.get("grid");
var tree = mini.get("tree");


function SetData(data) {
    var data = mini.clone(data);
    var action = data.action;
    var selected = data.selected;
    console.log(selected);
    // mask(grid,tree);//a:取消,b:锁定

    // if(data.selected != undefined) {
    //     selected = data.selected.split(',');
    // }else {
    //     selected = '';
    // }

    //加载列表
    grid.setData(data.refData);
    //默认选中
    grid.findRows(function (row) {
        for(var i=0;i<selected.length;i++) {
            if (row.cTableField == selected[i].cTableField) {
                grid.select(row);
            }
        }
    });

    for(var m=0;m<data.treeList.length;m++) {//未选中的不能出现在树中
        var item = data.treeList[m];
        var flag = false;
        for(var p=0;p<data.refData.length;p++) {
            var row = data.refData[p];
            if(row.formListId == item.id) {
                flag = true;
            }
            if(p == data.refData.length -1 && !flag) {//不存在
                for(var k=0;k<data.treeList.length;k++) {//其它相关节点设为根
                    if(data.treeList[k].pid == item.id) {
                        data.treeList[k].pid = -1;
                    }
                }
                data.treeList.splice(m,1);//删除m
            }
        }
    }


    tree.loadList(data.treeList, "id", "pid");

    // var tree_order = [];
    // var tree_data = grid.getSelecteds();

    // tree_data = reduce(tree_data,'formListId');//arr:去重的数组，type：去重的参照索引

    // //未设置过
    // for(var i=0;i<tree_data.length;i++) {
    //     tree_order.push({
    //         text: tree_data[i].cFormName,
    //         pid: "-1",
    //         id: tree_data[i].formListId
    //     });
    // }
    // console.log(tree_order);
    // //加载树
    // tree.loadList(tree_order, "id", "pid");

}

function onSelectionChanged(e) {
    var treeList = tree.getList();
    console.log(e.records);//操作的记录
    console.log(e.selecteds);//选中的记录
    if(e.select) {//选中操作，增加节点
        for(var i=0;i<e.records.length;i++) {
            var flag = false;
            if(treeList.length == 0) {
                addNode(e.records[i]);
            }else {
                for(var j=0;j<treeList.length;j++) {
                    if(e.records[i].formListId == treeList[j].id) {
                        flag = true;
                    }
                    if(j==treeList.length-1 && !flag) {//不存在，增加节点
                        addNode(e.records[i]);
                    }
                }
            }
        }
    }else {//取消选中，减少节点
        for(var p=0;p<e.records.length;p++) {//取消的节点
            for(var q=0;q<treeList.length;q++) {
                if(e.records[p].formListId == treeList[q].id) {

                    for(var k=0;k<treeList.length;k++) {
                        if(treeList[k].pid == treeList[q].formListId) {
                            treeList[k].pid = -1;
                        }
                    }
                    treeList.splice(q,1);
                }
            }
        }
        console.log(treeList)
        tree.loadList(treeList, "id", "pid");
    }
}

function addNode(info) {
    console.log(info);
    var node = tree.getSelectedNode();
    var newNode = {text: info.cFormName,id: info.formListId,cTableName: info.cTableName};
    tree.addNode(newNode, "after", node);
}
//确定按钮之前
function onTableOk() {

    var selecteds = grid.getSelecteds();
    var treeList = tree.getList();
    if(selecteds) {
        return {selecteds:selecteds,treeList: treeList}; //返回选中的数据给父组件
    }else {
        mini.alert('请选择数据！');
        return;
    }
}


//确定按钮
function onOk(e) {
    CloseWindow("ok");
}
//取消按钮
function onCancel(e) {
    CloseWindow("cancel");
}

function CloseWindow(action) {

    if (window.CloseOwnerWindow)
        return window.CloseOwnerWindow(action);
    else
        window.close();
}

function mask(a,b) {
    a.unmask();
    b.mask({
            html : "<div></div>",
            cls : "mini-panel",
            style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
    });
}